<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-button type="primary" size="default" @click="handelCreate">新增</el-button>
        </el-col>
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :show-overflow-tooltip="{ effect: 'light' }"
          >
            <el-table-column
              label="序号"
              type="index"
              :index="(index: number) => index * queryForm.pageIndex + 1"
              min-width="50"
            />
            <el-table-column label="设备编码" prop="deviceCode" min-width="250" />
            <el-table-column label="所属企业" prop="corpName" min-width="200" />
            <el-table-column label="所属项目" prop="projectName" min-width="200" />
            <el-table-column label="设备描述" prop="description" min-width="120" />
            <el-table-column label="状态" prop="statusName" min-width="80">
              <template #default="scope">
                <span v-if="scope.row.status == 0">
                  <el-text type="info"> {{ scope.row.statusName }} </el-text>
                </span>
                <span v-if="scope.row.status == 1">
                  <el-text type="success"> {{ scope.row.statusName }} </el-text>
                </span>
                <span v-if="scope.row.status == 2">
                  <el-text type="danger"> {{ scope.row.statusName }} </el-text>
                </span>
              </template>
            </el-table-column>

            <el-table-column label="审核人" prop="auditUserName" min-width="100" />
            <el-table-column label="审核时间" prop="auditTime" min-width="150" />
            <el-table-column label="禁用原因" prop="disableReason" min-width="100" />
            <el-table-column label="禁用时间" prop="disableTime" min-width="150" />
            <el-table-column label="创建时间" prop="createTime" min-width="150" />
            <el-table-column label="操作" fixed="right" max-width="260" min-width="160">
              <template #default="scope">
                <el-button v-if="scope.row.status == 0" link size="small" type="primary" @click="handelEdit(scope.row)">
                  修改
                </el-button>
                <el-button type="primary" link size="small" @click="handelDelete(scope.row)"> 删除 </el-button>
                <el-button
                  v-if="scope.row.status == 0"
                  type="primary"
                  link
                  size="small"
                  @click="handleAudit(scope.row)"
                >
                  审核
                </el-button>
                <el-button
                  v-if="scope.row.status == 2"
                  type="primary"
                  link
                  size="small"
                  @click="handelEnable(scope.row)"
                >
                  启用
                </el-button>
                <el-button
                  v-if="scope.row.status == 1"
                  type="primary"
                  link
                  size="small"
                  @click="handelDisable(scope.row)"
                >
                  禁用
                </el-button>
                <!-- <el-button type="primary" link size="small" @click="handelDetail(scope.row)"> 详情 </el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageIndex"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <add-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
    <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="sendClose"></detail>
  </div>
</template>
<script setup lang="ts">
import addForm from "./addForm.vue";
import detail from "./detail.vue";
import {
  QueryCardReaderDeviceList,
  DeleteCardReaderDevice,
  EnableCardReaderDevice,
  DisableCardReaderDevice,
  AuditCardReaderDevice,
} from "@/api/device/index";

const loading = ref(false);
const tableData = ref();

const dialogAddOrEdit = reactive<any>({
  visible: false,
});
const dialogDetail = reactive<any>({
  visible: false,
});

//新增
const handelCreate = function () {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认新增";
  dialogAddOrEdit.title = "新增";
};
//编辑
const handelEdit = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.id;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
};

//详情
const handelDetail = function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "详情";
};

//启用
const handelEnable = async function (row: any) {
  let res = await ElMessageBox.confirm("是否确认启用该项目？");
  if (res) {
    let res = await EnableCardReaderDevice(row.id);
    if (res.success) {
      ElMessage({
        type: "success",
        message: "启用成功",
      });
      handleQuery();
    }
    if (!res.success) {
      ElMessage({
        type: "error",
        message: res.msg,
      });
    }
  }
};

//禁用
const handelDisable = async function (row: any) {
  ElMessageBox.prompt("请输入禁用原因", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    inputPattern: /[^ \x20]+/,
    inputErrorMessage: "请输入禁用原因",
  })
    .then(({ value }) => {
      DisableCardReaderDevice(row.id, value).then((res) => {
        if (res.type == 0) {
          if (res.success) {
            ElMessage({
              type: "success",
              message: `禁用成功`,
            });
            handleQuery();
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
          }
        } else {
          ElMessage({
            type: "error",
            message: res.msg,
          });
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
};

//审核
const handleAudit = async function (row: any) {
  ElMessageBox.confirm("确定审核？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      AuditCardReaderDevice(row.id).then((res) => {
        if (res.type === 0) {
          if (res.success) {
            ElMessage({
              type: "success",
              message: res.msg,
            });
            handleQuery();
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
          }
        }
      });
    })
    .catch(() => {
      console.log("取消");
    });
};

const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  if (needRefresh) {
    handleQuery();
  }
};

//删除
const handelDelete = function (row: any) {
  ElMessageBox.confirm("确定删除该项目吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      DeleteCardReaderDevice(row.id).then((res) => {
        if (res.type === 0) {
          if (res.success) {
            ElMessage({
              type: "success",
              message: res.msg,
            });
            handleQuery();
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
          }
        }
      });
    })
    .catch(() => {
      console.log("取消");
    });
};

//查询条件
const queryForm = reactive({
  keywords: "",
  pageIndex: 1,
  pageSize: 10,
});
const total = ref(0);

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryCardReaderDeviceList(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageIndex = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}
:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
